<?php

if (!function_exists('get_time')) {
    /**
     * @param $time_type 1今天 2昨天 3本周 4上周 5近7天 6本月 7上月 8近30天 9本季度 10上季度 11本年 12上年
     * @param bool $timestamp_flag
     * @return array
     */
    function get_time($time_type, $timestamp_flag = false)
    {
        date_default_timezone_set('PRC');
        switch ($time_type){
            case '1':
                //今天
                $start_time = $timestamp_flag ? strtotime(date("Y-m-d 00:00:00")) : date("Y-m-d 00:00:00");
                $end_time   = $timestamp_flag ? time() : date("Y-m-d H:i:s");
                break;
            case '2':
                //昨天
                $start_time = $timestamp_flag ? strtotime(date("Y-m-d 00:00:00")) - 86400 : date('Y-m-d 00:00:00', strtotime('-1 day'));
                $end_time   = $timestamp_flag ? strtotime(date("Y-m-d 00:00:00")) - 1 : date('Y-m-d 23:59:59', strtotime('-1 day'));
                break;
            case '3':
                //本周
                $start_time = $timestamp_flag ? mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")) : date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
                $end_time   = $timestamp_flag ? time() : date("Y-m-d H:i:s");
                //$end_time   = $timestamp_flag ? mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")) : date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
                break;
            case '4':
                //上周
                $start_time = $timestamp_flag ? mktime(0,0,0, date('m'),date('d') - date('w') + 1 - 7, date('Y')) : date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")));
                $end_time   = $timestamp_flag ? mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y")) : date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y")));
                break;
            case '5':
                //近7天
                $start_time = $timestamp_flag ? strtotime('-7 day') : date('Y-m-d H:i:s', strtotime('-7 day'));
                $end_time   = $timestamp_flag ? time() : date("Y-m-d H:i:s");
                break;
            case '6':
                //本月
                $start_time = $timestamp_flag ? strtotime(date("Y-m-01 00:00:00",time())) : date("Y-m-01 00:00:00",time());
                $end_time   = $timestamp_flag ? time() : date("Y-m-d H:i:s");
                //$end_time   = $timestamp_flag ? strtotime(date("Y-m-t 23:59:59")) : date("Y-m-t 23:59:59");
                //$end_time   = $timestamp_flag ? mktime(23,59,59,date("m"),date("t"),date("Y")) : date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));
                break;
            case '7':
                //上月
                //$start_time = $timestamp_flag ? mktime(0, 0 , 0,date("m")-1,1,date("Y")) : date("Y-m-d H:i:s", mktime(0, 0 , 0,date("m")-1,1,date("Y")));
                //$end_time   = $timestamp_flag ? mktime(23,59,59,date("m") ,0,date("Y")) : date("Y-m-d H:i:s", mktime(23,59,59,date("m") ,0,date("Y")));
                $start_time = $timestamp_flag ? strtotime(date("Y-m-01 00:00:00",strtotime("-1 month"))) : date("Y-m-01 00:00:00",strtotime("-1 month"));
                $end_time   = $timestamp_flag ? strtotime(date("Y-m-t 23:59:59",strtotime("-1 month"))) : date("Y-m-t 23:59:59",strtotime("-1 month"));
                break;
            case '8':
                //近30天
                $start_time = $timestamp_flag ? strtotime('-30 day') : date('Y-m-d H:i:s', strtotime('-30 day'));
                $end_time   = $timestamp_flag ? time() : date("Y-m-d H:i:s");
                break;
            case '9':
                //本季度
                $season     = ceil(date("n") /3); //获取月份的季度
                $start_time = $timestamp_flag ? mktime(0,0,0,($season - 1) *3 +1,1,date('Y')) : date('Y-m-01 H:i:s',mktime(0,0,0,($season - 1) *3 +1,1,date('Y')));
                $end_time   = $timestamp_flag ? mktime(23,59,59,$season * 3,1,date('Y')) : date('Y-m-t H:i:s',mktime(23,59,59,$season * 3,1,date('Y')));
                break;
            case '10':
                //上季度
                $season     = ceil(date("n") /3); //获取月份的季度
                $start_time = $timestamp_flag ? mktime(0,0,0,($season - 2) * 3 +1,1,date('Y')) : date('Y-m-01 H:i:s',mktime(0,0,0,($season - 2) * 3 +1,1,date('Y')));
                $end_time   = $timestamp_flag ? mktime(23,59,59,($season - 1) * 3,1,date('Y')) : date('Y-m-t H:i:s',mktime(23,59,59,($season - 1) * 3,1,date('Y')));
                break;
            case '11':
                //本年
                $start_time = $timestamp_flag ? strtotime(date('Y-01-01 00:00:00')) : date('Y-01-01 00:00:00');
                $end_time   = $timestamp_flag ? strtotime(date('Y-12-31 23:59:59')) : date('Y-12-31 23:59:59');
                break;
            case '12':
                //上年
                $start_time = $timestamp_flag ? strtotime(date('Y-01-01 00:00:00',strtotime('-1 year'))) : date('Y-01-01 00:00:00',strtotime('-1 year'));
                $end_time   = $timestamp_flag ? strtotime(date('Y-12-31 23:59:59',strtotime('-1 year'))) : date('Y-12-31 23:59:59',strtotime('-1 year'));
                break;
            case '13':
                //近一个月
                $start_time = $timestamp_flag ? strtotime('-1 month') : date('Y-m-d H:i:s', strtotime('-1 month'));
                $end_time   = $timestamp_flag ? time() : date("Y-m-d H:i:s");
                break;
            case '14':
                //近三个月
                $start_time = $timestamp_flag ? strtotime('-3 month') : date('Y-m-d H:i:s', strtotime('-3 month'));
                $end_time   = $timestamp_flag ? time() : date("Y-m-d H:i:s");
                break;
            default :
                //默认今天
                $start_time = $timestamp_flag ? strtotime(date("Y-m-d 00:00:00")) : date("Y-m-d 00:00:00");
                $end_time   = $timestamp_flag ? time() : date("Y-m-d H:i:s");
                break;
        }

        return [
            'start_time' => $start_time,
            'end_time' => $end_time,
        ];
    }
}
